import { getEmployeeInfo } from '@/api/employeeApi';
import { getDepartmentById } from "@/api/departmentApi";

export default {
    data() {
        return {
            showPassword: false,
            search: { account: '0' },
            page: {},      // 员工信息
            departmentCache: {}, // 部门缓存
            loadingDepartment: false // 加载状态
        };
    },
    created() {
        this.loadData();
    },
    computed: {
        currentDepartmentName() {
            if (!this.page.departmentId) return '未知';
            return this.departmentCache[this.page.departmentId] || '加载中...';
        }
    },
    methods: {
        // 跳转到编辑页
        toUpdateEmployee() {
            this.$router.push({ path: '/Mine/UpdateMyInfo' });
        },

        // 加载部门名称
        async loadDepartmentName(departmentId) {
            if (!departmentId || this.departmentCache[departmentId]) return;

            this.loadingDepartment = true;
            try {
                const result = await getDepartmentById(departmentId);
                if (result.code === 200 && result.data) {
                    this.$set(this.departmentCache, departmentId, result.data.departmentName);
                } else {
                    this.$set(this.departmentCache, departmentId, '未知部门');
                }
            } catch (error) {
                console.error('获取部门信息失败:', error);
                this.$set(this.departmentCache, departmentId, '加载失败');
            } finally {
                this.loadingDepartment = false;
            }
        },

        // 加载数据
        async loadData() {
            try {
                // 1. 获取员工ID（从登录态）
                this.search.account = window.sessionStorage.getItem('empId');

                // 2. 调用API获取员工信息
                const empResult = await getEmployeeInfo(this.search.account);
                if (empResult.code === 200) {
                    this.page = empResult.data;

                    // 3. 加载部门名称
                    if (this.page.departmentId) {
                        await this.loadDepartmentName(this.page.departmentId);
                    }
                }
            } catch (error) {
                console.error('加载数据失败:', error);
                this.$alert('网络错误，请稍后再试', '提示', {
                    confirmButtonText: '确定'
                });
            }
        },

        // 其他格式化方法保持不变...
        formatDate(date) {
            if (!date) return '未知';
            const d = new Date(date);
            return d.toLocaleDateString('zh-CN');
        },

        formatRole(role) {
            const roles = {
                0: '普通员工',
                1: '部门经理',
                2: '总经理'
            };
            return roles[role] || '未知';
        },

        formatGender(gender) {
            const genders = {
                0: '男',
                1: '女',
                2: '未知'
            };
            return genders[gender] || '未知';
        },

        formatActiveStatus(isActive) {
            return isActive ? '在职' : '离职';
        },

        formatSalaryLevel(level) {
            return level ? `级别 ${level}` : '未设置';
        },

        formatAnnualLeave(days) {
            return days !== null && days !== undefined ? `${days} 天` : '未设置';
        }
    }
};